//
//  GYCategoryListVC.m
//  GYDemo
//
//  Created by Mrzhang on 2018/3/27.
//  Copyright © 2018年 mac. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "GYCategoryListVC.h"
#import "Head.h"
#import "GYMineLikeProductInfoVC.h"
#import "GYHomeSaleDetailVC.h"
#import "GYProductCateModel.h"
#import "GYCategoryViewModel.h"
#import "GYCategoryPickerView.h"
@interface GYCategoryListVC ()
{
    NSInteger currentPage;
}
@property (nonatomic,strong)GYCategoryViewModel *likeProductInfoviewModel;
@property(nonatomic,strong)NSMutableDictionary* paraDataSource;
@property(nonatomic,strong)GYChooseView* chooseView;
@property(nonatomic,strong)GYProductCateModel* CateModel;

@end
@implementation GYCategoryListVC
#pragma mark - getter and setter
-(GYProductCateModel *)CateModel{
    
    if (!_CateModel) {
        _CateModel=[GYProductCateModel shareInterface];
    }
    return _CateModel;
}
-(GYCategoryViewModel *)likeProductInfoviewModel
{
    if (!_likeProductInfoviewModel) {
        _likeProductInfoviewModel = [GYCategoryViewModel new];
    }
    return _likeProductInfoviewModel;
}
-(NSMutableDictionary *)paraDataSource{
    
    if (!_paraDataSource) {
        _paraDataSource=[[NSMutableDictionary alloc] init];
    }
    return _paraDataSource;
}
-(GYChooseView *)chooseView
{
    if (!_chooseView) {
        _chooseView=[[GYChooseView alloc] initWithFrame:WFCGRectMake(0, 0, 375, 45) chooseViewType:3];
        _chooseView.backgroundColor=[UIColor whiteColor];
    
        [WFFunctions WFUIaddBorderToView:_chooseView top:NO left:0 bottom:1 right:0 borderColor:RGBACOLOR(240, 240, 240, 1) borderWidth:0.5];
        @WeakSelf;
        _chooseView.chooseBack = ^(NSDictionary *paraDic) {
            for (NSString* key in paraDic.allKeys)
            {
                [selfp.paraDataSource setObject:paraDic[key] forKey:key];
            }
            currentPage=1;
            [selfp.paraDataSource setObject:@"1" forKey:@"page"];
            [selfp reloadData];
        };
        _chooseView.categoryback = ^(NSString *categoryStr) {
            currentPage=1;
            [selfp.paraDataSource setObject:@"1" forKey:@"page"];
            [selfp.paraDataSource setObject:categoryStr forKey:@"sort"];
            [selfp reloadData];
            
        };
    }
    return _chooseView;
}
#pragma mark - event
#pragma mark - method
-(void)setOriginData
{
    currentPage = 1;
    for (NSString* key in self.paraDataSource.allKeys) {
        if (![key isEqualToString:@"sort"])
        {
            [self.paraDataSource removeObjectForKey:key];
        }
    }
    if (![WFFunctions WFStrCheckEmpty:_passID]) {
        self.paraDataSource[@"protypeid"]=[NSString stringWithFormat:@"%@",_passID];
    }
    self.paraDataSource[@"page"]=@"1";
    self.paraDataSource[@"pagesize"]=@"10";
    [self reloadData];
}
-(void)initilization
{

    [self setOriginData];
    self.navigationItem.title = @"现货资源";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationController.navigationBar.shadowImage = [[UIImage new]rt_darkenWithLevel:0.3f];
    [self setNavgationTheme:0];
    [self.view addSubview:self.chooseView];
    [self.view addSubview:self.baseTable];
    self.baseTable.y=WFCGFloatY(45);
    [self.baseTable setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.baseTable setSeparatorColor: RGBACOLOR(233, 233, 233, 1)];
    [self.baseTable setBackgroundColor:[UIColor whiteColor]];
    [self.baseTable setTintColor:Main_Color_BG];
    self.baseTable.showsVerticalScrollIndicator=NO;
    self.baseTable.height = self.view.height - GY_Nav_height-WFCGFloatY(45)-self.bottomShelterHeight;
    @WeakSelf;
    self.baseTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        currentPage = 1;
        [selfp setOriginData];
        [selfp reloadData];
    }];
    self.baseTable.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
        if (selfp.likeProductInfoviewModel.canAddMore)
        {
            currentPage += 1;
            [selfp.paraDataSource setObject:[NSString stringWithFormat:@"%ld",currentPage] forKey:@"page"];
            [selfp reloadData];
        }else
        {
            [selfp.baseTable.mj_footer endRefreshingWithNoMoreData];
        }

    }];
    [self viewModelBand];
    //    [self.baseTable.mj_header beginRefreshing];
    [self reloadData];
//    if (_CateModel.dataSource.count==0) {
//        [_CateModel requestCategory];
//    }
}
-(void)viewModelBand
{
    @WeakSelf;
    GCBaseObservationModel *observation = [GCBaseObservationModel new];
    observation.observation = self.likeProductInfoviewModel;
    observation.keyPath = @"datas";
    observation.handler = ^(NSString *keyPath) {
        [selfp reloadView];
        if ([selfp.paraDataSource[@"page"] isEqualToString:@"1"]) {
            [selfp.baseTable reloadData];
        }

    };
    [self registObservation:observation];
    
    
    GCBaseObservationModel *observation1 = [GCBaseObservationModel new];
    observation1.observation = self.likeProductInfoviewModel;
    observation1.keyPath = @"totalCount";
    observation1.handler = ^(NSString *keyPath) {
        [selfp.chooseView setTotalCount:selfp.likeProductInfoviewModel.totalCount];
    };
    [self registObservation:observation1];
}
-(void)reloadView
{
    [self.baseTable reloadData];

}
-(void)reloadData
{
    [self.likeProductInfoviewModel requestDatas:self.paraDataSource];
}
#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    // Do any additional setup after loading the view.
    [self initilization];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.likeProductInfoviewModel.datas.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellID = @"CellID";
    GYMineLikeProductCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (cell == nil) {
        cell = [[GYMineLikeProductCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    @WeakSelf
    [cell.deleteBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
        [selfp tableView:tableView commitEditingStyle:UITableViewCellEditingStyleDelete forRowAtIndexPath:indexPath];
    }];
    GYMineLikeProductInfoModel *model = self.likeProductInfoviewModel.datas[indexPath.row];
    cell.model = model;
    [cell.deleteBtn setHidden:YES];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = WFCGFloatY(104);
    return height;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GYMineLikeProductInfoModel *model = self.likeProductInfoviewModel.datas[indexPath.row];
    GYHomeSaleDetailVC* gy=[[GYHomeSaleDetailVC alloc] init];
    gy.passID=model.id;
    [self.navigationController pushViewController:gy animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - delegate
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
